<?php

/**
 * alltosun.com 门店控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 刘德洪 (Liudh@alltosun.com) $
 * $Date: 20151124 $
 * $Id$
 */
class Action
{
    private $per_page = 20;

    public function __construct()
    {

    }

    /**
     * 显示列表
     */
    public function index()
    {
        $page = Request::Get('page_no', 1);
        $status = Request::getParam('status', 1);
        $progress = Request::getParam('progress', '-1');
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }

        if (isset($search_filter['serial']) && $search_filter['serial']) {
            $filter['serial'] = $search_filter['serial'];
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'] . ' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'] . ' 23:59:59';
        }

        if ($progress != '-1' || $status == 0) {
            $filter['status'] = $status;
        } else {
            $filter['1'] = 1;
        }

        $store_list = array();
        $store_count = _model('store')->getTotal($filter);
        if ($store_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($store_count)) {
                Response::assign('pager', $pager);
            }
            $store_list = _model('store')->getList($filter, ' order BY `add_time` DESC ' . $pager->getLimit($page));

            Response::assign('count', $store_count);
        }

        Response::assign('store_list', $store_list);
        Response::assign('status', $status);
        Response::assign('progress', $progress);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/store_list.html');
    }

    /**
     * 区域查询 选择终端类型
     */
    public function list_step1()
    {
        $page = Request::Get('page_no', 1);
        $status = Request::getParam('status', -1);
        $only_chain_shop = Request::getParam('only_chain_shop', 0);
        $only_chain_shop = Request::getParam('type', 0);

        //如果是合伙人，则
        //如果是经销商




        $arr = _model('store')->getAll('select type, count(*) as cnt from store where status != 0 group by `type`');
        if (!empty($arr)) {
            //重排
            foreach ($arr as $v) {
                $type = $v['type'];
                $arr2[$type] = $v['cnt'];
            }

            $all = 0;
            $ret[] = array(
                'type' => 0,
                'name' => '全国终端',
                'num'  => 0,
            );
            foreach (store_config::$type_arr as $type => $name) {
                if (isset($arr2[$type])) {
                    $ret[] = array(
                        'type' => $type,
                        'name' => $name,
                        'num'  => $arr2[$type],
                    );

                    $all += $arr2[$type];
                }
            }

            $ret[0]['num'] = $all;

        }

        return $ret;
    }
}
